<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="pic/blog-logo.png" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>
    <div class="content-edit">
        <div class="push">
            <input type="hidden" id="blogId">
            <input type="text" name="" id="title">
            <input type="button" value="更新文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content">##在这里写下一篇博客</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">

        // $(function () {
        //     var editor = editormd("editor", {
        //         width  : "100%",
        //         height : "550px",
        //         path: "blog-editormd/lib/"
        //     });
        // });

        // 更新博客
        function submit() {
           $.ajax({
               type: 'post',
               url: 'blog/update',
               contentType: 'application/json',
               data: JSON.stringify({
                   id: $('#blogId').val(),
                   title: $('#title').val(),
                   content: $('#content').val()
               }),
               success: function (ret) {
                   if (ret.code == 'SUCCESS' && ret.data == true) {
                       alert('编辑成功！')
                       location.href = 'blog_detail.html?blogId='+$('#blogId').val()
                   } else {
                       alert(ret.errMsg)
                   }
               }
           })
        }

        // 获取这个待修改的博客的原有信息
        function getBlogInfo() {
            $.ajax({
                type: 'get',
                url: 'blog/getBlogDetail'+location.search,
                success: function (ret) {
                    if (ret.code == 'SUCCESS' && ret.data != null) {
                        $('#title').val(ret.data.title)
                        // $('#content').html(ret.data.content)
                        $('#blogId').val(ret.data.id)
                    } else {
                        alert('获取博客信息失败！')
                    }
                    editormd("editor", {
                        width: "100%",
                        height: "550px",
                        path: "blog-editormd/lib/",
                        onload: function () {
                            this.watch();
                            this.setMarkdown(ret.data.content);
                        }
                    })
                }
            })
        }
        getBlogInfo();
    </script>
</body>

</html>